package com.sikao.app.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;

import com.sikao.app.app.MyApplication;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * Created by y50 on 2016/5/17.
 */
public class NetWorkUtil {
	public static boolean checkNetwork(boolean isShow) {
		ConnectivityManager connectivityManager = (ConnectivityManager) MyApplication.getInstance().getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
		if (networkInfo == null || !networkInfo.isAvailable()) {
			if(isShow){
				Toast.makeText(MyApplication.getInstance(), "网络连接失败，请检查网络", Toast.LENGTH_SHORT).show();
			}
			return false;

		} else if (networkInfo.getType() != ConnectivityManager.TYPE_WIFI) {
			//Toast.makeText(this, "当前网络连接使用手机流量!,建议使用Wifi!", Toast.LENGTH_LONG);
		}

		return true;
	}
	private String getPhoneIp() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
						//if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (Exception e) {
		}
		return "";
	}

	/** NETWORK_TYPE_CDMA 网络类型为CDMA
	* NETWORK_TYPE_EDGE 网络类型为EDGE
	* NETWORK_TYPE_EVDO_0 网络类型为EVDO0
	* NETWORK_TYPE_EVDO_A 网络类型为EVDOA
	* NETWORK_TYPE_GPRS 网络类型为GPRS
	* NETWORK_TYPE_HSDPA 网络类型为HSDPA
	* NETWORK_TYPE_HSPA 网络类型为HSPA
	* NETWORK_TYPE_HSUPA 网络类型为HSUPA
	* NETWORK_TYPE_UMTS 网络类型为UMTS*/
}
